package com.nemodynamic.cattivial.UIElement;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class CattivialBackground extends View {
	private Bitmap cattivialBG;
	private int translation = 100;
	private float circularPulse = 0;

	public CattivialBackground(Context context, int resource) {
		super(context);
		double scaleFactor;
		cattivialBG = BitmapFactory.decodeResource(getResources(), resource);
		int originalWidth = cattivialBG.getWidth();
		int originalHeight = cattivialBG.getHeight();
		System.out.println("oh="+originalHeight);
		System.out.println("ow="+originalWidth);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		int width = display.getWidth() + translation; // deprecated
		int height = display.getHeight()+translation; // deprecated
		if (originalHeight < originalWidth) {
			scaleFactor = ((double) height / originalHeight);
		} else {
			scaleFactor = ((double) width / originalWidth);
		}
	
		cattivialBG = Bitmap.createScaledBitmap(cattivialBG,
				(int) (originalWidth * scaleFactor),
				(int) (originalHeight * scaleFactor), false);
	}

	public CattivialBackground(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public CattivialBackground(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawARGB(0, 0, 0, 0);
		circularPulse += 0.015;
		if (circularPulse > (2 * Math.PI)) {
			circularPulse = 0;
		}
		float translateX = (float) Math.sin(circularPulse)*(translation/2) ;
		float translateY = (float) Math.cos(circularPulse+3)*(translation/2) ;
		float scale =(float) Math.cos(circularPulse) * 0.02f + 1.02f;
		canvas.scale(scale, scale,cattivialBG.getWidth()/2,cattivialBG.getHeight()/2);
		canvas.drawBitmap(cattivialBG, -translation/2 + translateX/2,-translation/2 +translateY/2, null);
		invalidate();
	}

}